package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.record.DelMonthRecReq;
import appapi.Request.record.MonthRecPageReq;
import appapi.Vo.record.AddMonthRecVo;
import appapi.Vo.record.EditMonthRecVo;
import appapi.Vo.record.InspectWarnVo;
import appapi.Vo.record.MonthInspWarnVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.config.MyLog;
import appapi.dto.MonthRecModel;
import appapi.dto.OnsiteRecModel;
import appapi.dto.bridge.MonthExportDto;
import appapi.entity.basic.UserInfo;
import appapi.entity.record.MonthInspectionModel;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.IStaffService;
import appapi.service.MonthinspectionService;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备月检业务接口
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/06/01 13:48
 */
@Api(tags = "设备月检")
@RestController
@RequestMapping(value = "/api/monthCheck")
public class MonthInspectionController {
    @Resource
    private MonthinspectionService iMonthinspection;
    @Resource
    IStaffService iStaffService;
    /**
     * 分页获取设备月检记录
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检-分页获取记录", notes = "月检-分页获取记录")
    @GetMapping(value = "/GetListByPage")
    public ResponseBean<MonthRecModel> GetListByPage(@CurrentUser UserInfo userInfo, MonthRecPageReq req) {
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            //如果什么都没传,则查到该账号的所属机构显示数据
            if (req.getOrgaPathIds() == null) {
                //拿到人员信息
                Map<String, Object> staffDto = iStaffService.SelectStaffDetail(userInfo.getUserid());
                String orgaPathIds = staffDto.get("orgaPathIds").toString();
                req.setOrgaPathIds(orgaPathIds);
            }
            PageInfo pageInfo = iMonthinspection.page(req);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备月检记录异常");
        }
    }


    /**
     * 获取设备月检记录列表
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检-获取设备记录列表", notes = "月检-获取设备记录列表")
    @GetMapping(value = "/GetMonthInspectionList")
    @ApiImplicitParam(paramType = "query", name = "equipId", dataType = "Long", value = "设备id")
    public ResponseModel GetMonthInspectionList(Long equipId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {

            List<Map<String, Object>> list = iMonthinspection.SelectList(equipId);

            responseModel.setData(list);
            responseModel.setRcount(list.size());
        } catch (BusinessException exception) {

            responseModel.setMessage("获取设备月检记录列表异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    @ApiOperation(value = "月检记录导出", notes = "月检记录导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/MonthRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    public ResponseBean MonthRecExport(MonthRecPageReq req, HttpServletResponse response) {
        try {
            List<MonthExportDto> list = iMonthinspection.getLists(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "月检记录", "月检记录sheet", MonthExportDto.class, "月检记录", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("月检记录导出异常");
        }
    }


    /**
     * 获取设备月检记录详情
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检--获取记录详情", notes = "获取记录详情")
    @GetMapping(value = "/GetMonthInspectionDetail")
    @ApiImplicitParam(paramType = "query", name = "monthId", dataType = "Long", value = "记录id")
    public ResponseBean<MonthInspectionModel> GetMonthInspectionDetail(Long monthId) {
        try {
            MonthInspectionModel resData = iMonthinspection.getDetail(monthId);
            return ResponseBean.success(resData);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取记录详情异常: ".concat(exception.getMessage()));
        }
    }


    /**
     * 添加月检记录
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检-添加月检记录", notes = "月检-添加月检记录")
    @PostMapping(value = "/AddMonthInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "vo", dataType = "object", value = "月检参数")
    })
    @MyLog(title = "添加月检记录",businessType = BusinessTypeEnum.insert)
    public ResponseBean AddMonthInspection(@CurrentUser UserInfo userInfo,@RequestBody AddMonthRecVo vo) {
        try {
            vo.setStaffId(userInfo.getUserid());
            vo.setCreateUser(userInfo.getUsername());
            int iRet=  iMonthinspection.InsertInfo(vo);
            if(iRet>0) {
                return ResponseBean.success();
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail("添加月检记录异常");
        }
    }

    /**
     * 编辑月检记录
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检-编辑月检记录", notes = "月检-编辑月检记录")
    @PostMapping(value = "/EditMonthInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "vo", dataType = "object", value = "月检参数")
    })
    @MyLog(title = "编辑月检记录",businessType = BusinessTypeEnum.update)
    public ResponseBean EditMonthInspection(@CurrentUser UserInfo userInfo,@RequestBody EditMonthRecVo vo) {
        try {
            vo.setStaffId(userInfo.getUserid());
            vo.setCreateUser(userInfo.getUsername());
            int iRet = iMonthinspection.UpdateInfo(vo);
            if (iRet > 0) {
                return ResponseBean.updateSuccess();
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail("编辑月检记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 删除月检记录
     *
     * @return
     * @author lc 2023/06/01
     */
    @ApiOperation(value = "月检-删除月检记录", notes = "月检-删除月检记录")
    @PostMapping(value = "/DeleteMonthInspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "req", dataType = "object", value = "月检记录id")
    })
    @MyLog(title = "删除月检记录",businessType = BusinessTypeEnum.update)
    public ResponseBean DeleteMonthInspection(@RequestBody DelMonthRecReq req) {
        try {
            int iRet = iMonthinspection.DeleteInfo(req.getMonthId());
            if (iRet > 0) {
                return ResponseBean.deleteSuccess();
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail("删除月检记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 获取月检统计
     *
     * @return
     * @author djq 2023/06/06
     */
    @ApiOperation(value = "巡检--获取月检统计", notes = "巡检--获取月检统计")
    @GetMapping(value = "/GetMonthWarn")
    public ResponseBean GetWarn(@CurrentUser UserInfo userInfo) {
        try {
            MonthInspWarnVo resData = iMonthinspection.GetWarn(userInfo);
            return ResponseBean.success(resData);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取月检统计异常: ".concat(exception.getMessage()));
        }
    }

}
